//---------------------------------------------------------------------------

#include "cod_arbori.h"
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
//---------------------------------------------------------------------------
LPCTSTR ClsName = "FundApp";
LPCTSTR WndName = " Arne Andersson -Tree";
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
			      WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------

HWND hWnd; 
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK DlgProcCONTROL(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK DlgProcSTEPS(HWND hWndDlg1, UINT Msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK Wndprincipal(HWND hwnd01, UINT Msg, WPARAM wParam, LPARAM lParam);

//---------------------------------------------------------------------------



INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
	MSG        Msg;
	HWND       hWnd;
	WNDCLASSEX WndClsEx;

	// Create the application window
	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = Wndprincipal;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_CROSS);
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.lpszMenuName  = (LPCSTR)IDR_MENU2;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	// Register the application
	RegisterClassEx(&WndClsEx);

	// Create the window object
	hWnd = CreateWindowEx(0,
                          ClsName,
                          WndName,
               			  WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
               			  CW_USEDEFAULT,
                          300,
               			  300,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
	
	// Find out if the window was created
	if( !hWnd ) // If the window was not created,
		return FALSE; // stop the application



	DialogBox(hInstance,LPCTSTR  (IDD_MAINDIALOG),
	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));

	while( GetMessage(&Msg, NULL, 0, 0) )
	{
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
	}

	return Msg.wParam;
}



LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	HINSTANCE hInstance=NULL;
	
	switch(Msg)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		switch(wParam)
		{
		case IDCANCEL:
			EndDialog(hWndDlg, 0);
			PostQuitMessage(WM_QUIT);
			return TRUE;
		case IDD_STARTB:
			EndDialog(hWndDlg, 0);

			hwndl=CreateDialog (NULL, 
                            MAKEINTRESOURCE (IDD_DIALOGCONTROL), 
                            hWnd, 
                            reinterpret_cast<DLGPROC>(DlgProcCONTROL));
			hwndl1=CreateDialog (NULL, 
                            MAKEINTRESOURCE (IDD_STEPS), 
                            hWnd, 
                            reinterpret_cast<DLGPROC>(DlgProcSTEPS));
			ShowWindow(hwndl,SW_SHOW);
			SendMessage(GetDlgItem(hwndl,IDC_EDIT) ,EM_LIMITTEXT,WPARAM(4),0);
			ShowWindow(hwndl1,SW_SHOW);
			hwnd01 = CreateWindowEx(WS_EX_CONTEXTHELP,
                          ClsName,
                          WndName,
               			  WS_OVERLAPPEDWINDOW,
                          5,
               			  153,
                          1336,
               			  560,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
			ShowWindow(hwnd01,SW_SHOW);

		

			aanod *p=(aanod *)malloc(sizeof(aanod));
			p->left=p->right=NULL;
			strcpy(p->info,"10000");

			Insert(Thead, p);
			
			return TRUE;

		}
		break;
	}

	return FALSE;
}
//---------------------------------------------------------------

 LRESULT CALLBACK Wndprincipal(HWND hwnd01, UINT Msg, WPARAM wParam, LPARAM lParam)

 {
	
	switch(Msg)
	{
		case WM_COMMAND:
					switch(wParam)
					{
						case ID_TEORIE: 
							MessageBox(hwnd01,"In cazul arborilor AA trebuiesc respectate doar doua reguli pentru a mentine echilibrul in arbore.Aceste doua cazuri sunt rezolvate cu ajutorul a doua operatii : skew si split.\n\nSKEW:\nOperatia skew inlatura cazul in care avem o legatura orizontala la stanga efectuand o rotatie la dreapta. Dupa skew nu trebuie schimbat nivelul niciunui nod deoarece aceasta transforma legatura orizontala stanga in una spre dreapta, aceasta nefiind un caz special.\n\nSPLIT:\nDaca dupa skew avem doua legaturi orizontale consecutive spre drepta atunci trebuie aplicata operatia split care efectueaza o rotatie spre stanga si incrementeaza nivelul nodului parinte. Split-ul dupa cum se observa este inversul operatiei skew.\n\n","Skew / Split",MB_OK);		
							break;
						
					}
					break;
	case WM_DESTROY:
		ShowWindow(hwndl,SW_HIDE);
		ShowWindow(hwndl1,SW_HIDE);
		MessageBox(hwnd01,"Proiect realizat de :\n \n               Murariu Roger [Rgr]\n               Raceanu Razvan[Rzv] \n\n                                     Supervised by Pura Mihai","SDA-AA Trees",MB_OK);
		ShowWindow(hwnd01,SW_NORMAL);
		PostQuitMessage(WM_QUIT);
		break;

	case WM_PAINT:
		{
			InvalidateRect(hwnd01,NULL,FALSE);
			UpdateWindow(hwnd01);
			hdc=BeginPaint(hwnd01,&Ps);
			RECT R;
			HBRUSH hbr;
			SetRect(&R,10,9,1325,507);
			hbr=CreateSolidBrush(RGB(255,255,255));
			FillRect(hdc,&R,hbr);

			UpdateWindow(hwndl);
			UpdateWindow(hwndl1);
			if (gasitt) // cazul pt search sa coloreze cu verde
			{
				UpdateWindow(hwnd01);
				RECT r,r1;
				r.left=8; r.top=13; r.right=48; r.bottom=33;
				r1.left=8; r1.top=36; r1.right=48; r1.bottom=66;
				
				DrawText(hdc,"Info",4,&r,DT_CENTER);
				
				SetTextColor(hdc,RGB(255,81,94));
				DrawText(hdc,"Level",5,&r1,DT_CENTER);
				SetTextColor(hdc,RGB(0,0,0));

				afisarearb(Thead,nodul);
				UpdateWindow(hwnd01);
				EndPaint(hwnd01,&Ps);
			}
			else if (gasit) // caz normal
			{		
			UpdateWindow(hwnd01);
			    
				///printare
				initrect();
				encodearb(Thead,Thead,"-");

			
				RECT r,r1;
				r.left=8; r.top=13; r.right=48; r.bottom=33;
				r1.left=8; r1.top=36; r1.right=48; r1.bottom=66;
				
				DrawText(hdc,"Info",4,&r,DT_CENTER);
				
				SetTextColor(hdc,RGB(255,81,94));
				DrawText(hdc,"Level",5,&r1,DT_CENTER);
				SetTextColor(hdc,RGB(0,0,0));

				afisarearb(Thead,Thead);
				UpdateWindow(hwnd01);
				EndPaint(hwnd01,&Ps);
				}
			
		}
		break;
	default : 
		return DefWindowProc(hwnd01,Msg,wParam,lParam);
		
	}
	return 0;
 }

//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProcCONTROL(HWND hWndDlg1, UINT Msg, WPARAM wParam, LPARAM lParam)
{

	HINSTANCE hInstance=NULL;
	switch(Msg)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		switch(wParam)
		{
		case IDCANCEL:
			EndDialog(hWndDlg1, 0);
			PostQuitMessage(WM_QUIT);
			return TRUE;
		case IDC_INSERT:
			{
			gasit=1;
			char s01[5];
			
			GetDlgItemText(hWndDlg1,IDC_EDIT,s01,5);
			if (!integer(s01)) MessageBox(hwndl,"ATENTIE ! Trebuie sa inserati un numar format doar din cifre [>0]","IMPORTANT",0);
			if (integer(s01))
			{
				aanod *p=(aanod *)malloc(sizeof(aanod));
				p->left=p->right=NULL;
				strcpy(p->info,s01);
			
				free(buff);	
				free(buffer);		
				buffer=(char *)malloc(10000*sizeof(char));
				buff=(char *)malloc(1000*sizeof(char));
				sprintf(buffer,"Urmarim pasii pe care ii facem pentru a efectua inserarea nodului [%s] ",s01);									
			

				UpdateWindow(hwndl1);
				Insert(Thead, p);
				encodearb(Thead,Thead,"-");
			
				strcat(buffer,"__SFARSIT");
				SetDlgItemText(hwndl1,IDD_STEPS,buffer);
				UpdateWindow(hwndl1);
				MessageBox(hwnd01,"Pentru a intelege mai bine inserarea urmariti pasii si apoi apasati ok pentru a afisa arborele in forma finala[dupa inserare]","Intelege mai bine ",MB_OK);
				SendMessage(hwnd01,WM_PAINT,(WPARAM )hdc,0);
			}
			}
			break;
	
		case IDC_SEARCH:
			{
			char s01[5];
			int h=0;
			gasitt=false;
			GetDlgItemText(hWndDlg1,IDC_EDIT,s01,5);
			nodul=(aanod *)malloc(sizeof(aanod));
			nodul->left=nodul->right=NULL;
			strcpy(nodul->info,s01);
			searchnod(Thead->left,nodul,&h);
			 
			if (gasitt)
			{
				SetDlgItemText(hwndl1,IDD_STEPS,"Nodul cautat a fost gasit -cel colorat cu verde ");
				UpdateWindow(hwndl1);
				SendMessage(hwnd01,WM_PAINT,(WPARAM )hdc,0);
			}
			else
				{		
					SetDlgItemText(hwndl1,IDD_STEPS,"Nodul cautat nu a fost gasit");
					UpdateWindow(hwndl1);
					SendMessage(hwnd01,WM_PAINT,(WPARAM )hdc,0);
				}
			gasitt=false;
			}
			break;
		case IDC_RANDOM:
				{
					random();
				}
				break;
		case IDC_CLEAR:
			{
				ran=0;
				clearmemory(Thead->left,2);
				Thead->left=NULL;
				SendMessage(hwnd01,WM_PAINT,(WPARAM )hdc,0);
			}
			break;
		}
		break;
	
	}

	return FALSE;
}
//---------------------------------------------------------------------------

LRESULT CALLBACK DlgProcSTEPS(HWND hWndDlg1, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	HINSTANCE hInstance=NULL;
	switch(Msg)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		switch(wParam)
		{
		case IDCANCEL:
			EndDialog(hWndDlg1, 0);
			PostQuitMessage(WM_QUIT);
			return TRUE;
	
		}
		break;
	}

	return FALSE;
}
//---------------------------------------------------------------------------